Fly3D SDK

Tutorials

Home

Tutorial 11

Creating a player object (observer)

· Introduction

After finishing this tutorial you will have created a new game object that can be used as a player. The object will move around the level aquiring keys and mouse to guide its movement.

· Visual C++ operations

1) Open the Visual C++ Fly3D Plug-ins Workspace

2) Select the menu option File/New to create a new project

3) Select the projects tab and the Fly3D Plug-in Wizard

4) Set the project location to the flysdk\plugin path and name the project 'observer'. Make sure to select 'add to curent workspace' to keep all Fly3D plug-in projects in a single workspace.

5) On the first wizard step, add a single class named 'observer'

6) On the second wizard step, add three parameters:

name: 'rotvel'     type: float
name: 'mousevel'   type: float
name: 'moveforce'  type: float
name: 'maxvel'     type: float
name: 'veldamp'    type: float

7) On the third wizard step, select the Fly3D SDK path and click Finish.

8) You should now be able to compile the plug-in with no errors

9) Open the observer.cpp file and put the following code over fly_message case FLYM_DRAWSCENE:

case FLYM_DRAWSCENE:
     // draws scene viewed from camera
     flyengine->set_camera(flyengine->cam);
     flyengine->draw_bsp();
     break;

10) Make the observer::init() fuction as follows:

void observer::init()
{
     // initialize observer at random position in level
    if (flyengine->get_random_point(pos,10)==0)
         pos.null();
}

11) Replace the observer step function with the following code:

int observer::step(int dt)
{
  if (node==0)     // if not in bsp (outside level on last frame)
     add_to_bsp(); // add it to the bsp as it might be inside now

  check_keys(dt); // process keys

  // damp velocity
  float len=vel.length();
  if (len<0.1f)
      vel.null();
  else
  {
      vel/=len;
      len-=dt*veldamp;
      if (len>maxvel)
          len=maxvel;
      if (len<0.0f)
          len=0.0f;
      vel*=len;
  }

  life=dt; // keep alive
  return particle::step(dt); // moves as particle
}

12) add the check_keys function as follows (also add its prototype to the observer.h file):

void observer::check_keys(int dt)
{
  // direct input keys
  unsigned char *diks=directx->diks;

  //  mouse smooth
  static int lastmouse[2][2]={ { 0,0 },{ 0,0 } },lm=0;
  float mousedx=(directx->dims.lX+lastmouse[0][0]+lastmouse[1][0])/3.0f;
  float mousedy=(directx->dims.lY+lastmouse[0][1]+lastmouse[1][1])/3.0f;
  lastmouse[lm][0]=directx->dims.lX;
  lastmouse[lm][1]=directx->dims.lY;
  lm=!lm;

  // process keys
  if (diks[0x38]) // ALT key
  {
    if (diks[0xcb]) // left arrow
        vel-=X*(moveforce*dt);
    if (diks[0xcd]) // right arrow
        vel+=X*(moveforce*dt);
    if (diks[0xc8]) // up arrow
        vel+=Y*(moveforce*dt);
    if (diks[0xd0]) // down arrow
        vel-=Y*(moveforce*dt);
    if (diks[0x1f]) // S key
        vel-=Z*(moveforce*dt);
    if (diks[0x2d]) // X key
        vel+=Z*(moveforce*dt);
  }
  else
  {
    if (diks[0xc8]) // up arrow
        rotate(-dt*rotvel,X);
    if (diks[0xd0]) // down arrow
        rotate(dt*rotvel,X);
    if (diks[0xcb]) // left arrow
        rotate(dt*rotvel,Y);
    if (diks[0xcd]) // right arrow
       rotate(-dt*rotvel,Y);
    if (diks[0x10]) // Q key
       vel-=X*(moveforce*dt);
    if (diks[0x12]) // E key
       vel+=X*(moveforce*dt);
    if (diks[0x1f]) // S key
       vel-=Z*(moveforce*dt);
    if (diks[0x2d]) // X key
       vel+=Z*(moveforce*dt);
  }

  if (diks[0x1e]) // A key
      rotate(dt*rotvel,Z);
  if (diks[0x20]) // D key
      rotate(-dt*rotvel,Z);

  if (mousedx) // mouse X
      rotate(-mousedx*mousevel,Y);
  if (mousedy) // mouse Y
      rotate(mousedy*mousevel,X);
}

12) Use the ALT-F7 key to edit the project settings. On the link tab, change the output file name to '../observer.dll'. This will save the compiled dll to the flysdk\plugin directory with all other Fly3D plug-ins (make sure to change this option for the release and debug versions).

13) Compile the tutorial (flysdk\plugin\observer.dll should be built).

14) You can now add this plugin to a level and set the global engine parameter camera and player to point to a active instance of the observer class. It will allow you to fly around the level with ease.

· flyEditor operations

1) Open the flyEditor.exe and click the new button

2) Save the new file (objesrver.fly) to the same directory as the file from the previous tutorials (flysdk\data\turorial).

3) Specify the BSP file to be used in the level selecting the global tree item and double clicking on the bspfile list view item. This will show an open file dialogue and you must select the BSP from turorials 1 or 2.

4) Now we need to create a new observer object from the observer.dll. This will be the player.
Insert the observer.dll to the level by right clicking the tree item Plugins and selecting insert.
Right click the new observer tree item and select insert from the popup menu.
A new observer object will be created and you can set its name to a chosen name like 'myplayer'.

5) Fill in the following observer object properties for your new player:

Parameter Value Description
colflag 1 sets collision on/off
mass 1 sets player mass (in this case >0)
radius 20 sets player collision radius to 2m (player collides as a sphere)
bump 1 bouncing factor (0.0 to 1.0)
friction 1 sliding factor (0.0 to 1.0)
active 1 activate on startup
rotvel .1 velocity for keybord rotation (arrows keys)
mosevel .1 velocity for mouse rotation
moveforce .01 force applyed for movement (S,X and slide keys)
maxvel .5 maximum velocity for the observer
veldump .001 damping factor for observer velocity

6) Activate an instance of the player by selectig the player and clinking the activate button on the tool bar (this must be done or you will not be able to set the camera and player global parameters to point to the observer as they can only point to active instances of objects).

7) Set the following global parameters by selecting the Glabal tree view item:

Parameter Value Description
viewmindist 10 sets near plane distance
viewmaxdist 1500 sets far plane distance
camangle 60 set camera view angle
camera myplayer set camera object
player myplayer set player object
mapmode 15 view all mapping layers

8) Now you are ready to fly around. Turn the render on by selecting OpenGL on the tool bar. Start simmulatig clicking the Simmulate tool bar button or using the F4 key. You should see the level from the observers point of view in the render window.

9) Select the render window by clicking on it (or use the TAB key) and you should be able to move around using the S,X,ARROW keys